﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 测试图片上传和表单提交
    /// </summary>
    public class UploadFileController : Controller
    {
        public class req
        {
            public string reqs { get; set; }

            public string name { get; set; }
        }
        // GET: UploadFile
        public ActionResult Index([FromBody] req o)
        {
            if (true)
            {
                var se = Request.Files;
                var seq = Request.InputStream;
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                using (System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream))
                {

                    byte[] byts = new byte[Request.InputStream.Length];
                    Request.InputStream.Read(byts, 0, byts.Length);
                    string req = System.Text.Encoding.GetEncoding("gb2312").GetString(byts);
                    req = Server.UrlDecode(req);
                    var sea = byts.ToString();
                }

            }
            var usernamea = Request["username"];

            var s = o;
            var name = Request["name"];
            var reqs = Request["reqs"];
            var re = Request.Form[0];
            var e = Request.ReadEntityBodyMode;
            var img = Request["img"];
            var ds = Request["name"];
            var iaamg = Request["pwd"];
            var st = Request.InputStream;
            if (name == "lpudding")
            {
                return new JsonResult()
                {
                    Data = new
                    {
                        status = 1,
                        msg = "保存成功"
                    },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                return new JsonResult()
                {
                    Data = new
                    {
                        status = 0,
                        msg = "用户名不存在"
                    },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
        }
        public ActionResult Image()
        {

            var usernamea = Request["username"];
            //var uploadimg = Request.Files["uploadimg"];
            var sde = Request["sde"];
            var file = Request.Files;

            if (file != null && file.Count > 0)
            {
                var name = file[0].FileName;
                var path = "touxiang/" + DateTime.Now.ToString("yyyyMMdd") + "/";
                var pypath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + path;
                var filename = Guid.NewGuid().ToString("n") + ".png";
                if (!System.IO.Directory.Exists(pypath))
                {
                    System.IO.Directory.CreateDirectory(pypath);
                }
                file[0].SaveAs(pypath + filename);
                var url = Request.Url.Scheme + "://" + Request.Url.Authority + System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + path + filename;
                return new JsonResult()
                {
                    Data = new
                    {
                        status = 0,
                        msg = "用户名不存在",
                        url = url
                    },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }

            var ps = Request["img"];
            var n = Request["name"];
            return View();
        }
    }
}